let indexModule = (function () {
    let $baseBox = $('.baseBox'),
        $baseBoxText = $baseBox.find('span'),
        $baseSignout = $baseBox.find('a'),
        $itemBoxs = $('.itemBox'),
        $itemBoxList = [],//用来存储显示的权限部门
        $customerall = $('#customerall'),
        itemArr = ["userhandle", "departhandle", "jobhandle", "customerall", "customermy"],
        $menuBox = $('.menuBox'),
        $navAs = $('.navBox>a'),
        $iframeBox = $('.iframeBox');
    // console.log($navBox,$navAs);

    // 基于发布订阅管理我们获取到个人信息和权限信息后要处理的事情
    let $plan = $.Callbacks();

    // $plan显示欢迎的基本信息
    $plan.add((power, baseInfo) => {
        $baseBoxText.html(`您好，${baseInfo.name || ''}`);
        $baseSignout.click(async () => {
            let result = await axios.get('/user/signout');
            if (result.code == 0) {
                localStorage.clear();
                window.location.href = './login.html';
                return;
            }
            alert('小主，当前网络繁忙，您稍后再试')
        })
    });
    // 控制显示权限信息
    $plan.add(power => {
        let index = [];
        if (power.indexOf('|') === -1) {
            index.push(itemArr.indexOf(power))
        } else {
            power.split('|').forEach((element, i) => {
                if ((a = itemArr.indexOf(element)) !== -1) {
                    index.push(a)
                }
            });
        }
        // 显示权限部门
        index.forEach(item => {
            $itemBoxList[item] = $itemBoxs[item];//保存权限
            if (item < 3 || item == 4) {
                $itemBoxs[item].style.display = 'block';
            } else if (item == 3) {

                $itemBoxs[item].style.display = 'block';
                $customerall.css("display", "block");
            }

        })
    })

    // 控制组织结构和客户管理切换效果和权限校验
    function hadleGroup(index) {
        // 先把itemboxs分组
        let $group1 = $itemBoxList.filter((item, index) => {
            // console.log(item,index);
            return (index === 3 || index === 4) && item != undefined;
        });
        let $group2 = $itemBoxList.filter((item, index) => {
            return (index === 0 || index === 1 || index === 2) && item != undefined;
        });
        // console.log($group1, $group2);
        if (index === 0) {
            $group2.forEach(item => { item.style.display = 'none' });
            $group1.forEach(item => { item.style.display = 'block' });
        } else {
            $group2.forEach(item => { item.style.display = 'block' });
            $group1.forEach(item => { item.style.display = 'none' });
        }
    }
    $plan.add(power => {

        // 控制默认显示哪一个
        let initialIndex = 0;
        if(!power.includes('customer')) {initialIndex = 1;}
        $navAs.eq(initialIndex).addClass('active').siblings().removeClass('active');
        hadleGroup(initialIndex);
        // 点击控制切换
        // console.log($navAs);

        $navAs.click(function () {
            let $this = $(this),
            index = $this.index(),
            text = $this.html().trim();
            if ((text === '客户管理') && !/customer/.test(power) || (text === '组织结构') && !(/userhandle|departhandle|jobhandle/.test(power))) {
                alert('无权限访问！');
                return;
            }
            if (index === initialIndex) return;
            $this.addClass('active');
            $navAs.eq(initialIndex).removeClass('active');
            hadleGroup(index);
            initialIndex = index;
        })

    })

    //权限处理（控制iframe嵌套的页面显示）
    $plan.add(power=>{
        let url = './page/customerlist.html?lx=my';
        if(!power.includes('customer')){
            url = './page/customerlist.html'
        }
        $iframeBox.attr('src',url);
    })

    return {
        async init() {
            // 1.验证当前用户是否登录
            let result = await axios.get('/user/login');
            if (result.code != 0) {
                // 未登录
                alert('小主，您还没有登录哦，请先登录~~');
                window.location.href = './login.html';
                return;
            }
            // 2.获取当前登录用户的个人信息和权限信息
            /* let loginInfo = await axios.get('/user/info/');//个人信息
            let userPower = await axios.get('/user/power');//获取权限信息，不过这两个应该是并行请求，所以用axios.all比较合适。 */
            let [power, baseInfo] = await axios.all([
                axios.get('/user/power'),
                axios.get('/user/info')
            ]);
            power.code == 0 ? power = power.power : null;
            baseInfo.code == 0 ? baseInfo = baseInfo.data : null;
            // console.log(power, baseInfo);

            // 3.通知计划表重点任务执行
            $plan.fire(power, baseInfo);

        }
    }
})();

indexModule.init();